//prepare summary statistics for analysis sample
use "$temp/psid_estimation_sample", clear

cap log close
log using "$output/Table_A8", replace
tab age
tab age if race == 1
tab age if race == 2
log close


gen marst = (m>1)
gen college = e-1
gen lfp = (h>0)
gen exp = x-1
gen child = (ac>1)
replace age = age + 21


//5 subgroups: full sample, college/no college (within, age 22/23 and age 35/34)

/*
//interesting things: 
hours worked 
marital status 
husband wage 
lfp 
child present
years of experience
sample size
*/
gen mig = (l_prime<11)

//flag for having ever moved
bys uniqid: egen mig_ever = max(mig)

replace lfp = lfp * 100
replace marst = marst * 100
replace child = child * 100
replace college = college * 100

lab var lfp "LFP Rate"
lab var wage "Hourly Wage"
lab var marst "Share Married"
lab var exp "Years of Experience"
lab var child "Young Child Present"
replace wage = . if wage == 0
//lab var nchild "Number of Children"

****baseline stats
eststo base_young: estpost su lfp exp wage marst child college if (age<=30)
eststo base_old: estpost su lfp exp wage marst child college if (age>30)
eststo white_young: estpost su lfp exp wage marst child college  if race==1 & (age<=30)
eststo white_old: estpost su lfp exp wage marst child college   if race==1 & (age>30)
eststo black_young: estpost su exp lfp wage marst child college  if race==2 & (age<=30)
eststo black_old: estpost su lfp exp wage marst child college  if race==2 & (age>30)
esttab base_young base_old white_young white_old black_young black_old using "$output/Table_A9a.tex", replace cell(mean(fmt(2)) sd(par fmt(2))) label


****migration statistics
/*
migration:
-migration rate (with and without kids, husband)
-percent ever-movers
-home migration rate (with, without parents) and parent migration rate
*/
drop if l_prime == 99 | age == 40 //kill unobserved moves to be precise

//generation of variables
gen parent_mig = .
replace parent_mig = 0 if mig & l_prime != 1
replace parent_mig = 1 if mig & l_prime == 1

tab parent_mig if mig

local commands `""su mig" "su mig if child" "su mig if marst" "su mig_ever" "su parent_mig if mig""'

replace mig = mig*100
replace parent_mig = parent_mig * 100
replace mig_ever = mig_ever*100

//summary stats (note: couldn't find a nice way to do this in estout, but this works decently)
//THIS IS THE SECOND PART OF TABLE AIX
cap log close
log using "$output/Table_A9b", replace
qui{
foreach command in `commands'{
	//whole sample
	`command'
	local base = round(`r(mean)', .01)
	local base_sd = round(`r(sd)', .01)
	
	//white subsample
	if strpos("`command'", " if "){\
		`command' & race==1	
	}
	else{
		`command' if race==1
	}
	local coll = round(`r(mean)', .01)
	local coll_sd = round(`r(sd)', .01)
	
	//black subsample
	if strpos("`command'", " if "){\
		`command' & race==2	
	}
	else{
		`command' if race==2
	}
	local ncoll = round(`r(mean)', .01)
	local ncoll_sd = round(`r(sd)', .01)

		//report
	noi di as result " & `base' & `coll' & `ncoll' \\"
	noi di as result " & (`base_sd') & (`coll_sd') & (`ncoll_sd') \\"
}
}
log close